Payworks - PayButton swift version

Rajai Kumar
Nerd For Tech
Published in
3 min readJan 30, 2021

--

Integrating the Payworks Payment Gateway — PayButton in Objective-C might be an easy job since they have their documentation in Objective-C. I had to convert the code from Objective-C to swift.

It was not that hard at the time. To be frank, it was fun. Now they have a page for swift. But still I wanted to share the work with other devs because there is not much in the community regarding Payworks integration in swift for iOS.

Let’s begin…

Step 1:

First you need to deal with the pods. Put the below in the pod file.

source 'https://github.com/CocoaPods/Specs.git'
source 'https://bitbucket.org/payworks/io.payworks.repo.pods.git'
use_frameworks!

target :"<your-app-target>" do
pod 'payworks', ''
pod 'payworks.paybutton', ''
end

Install the pods.

Step 2:

Create the mPosUi object as shown below. We currently have .TEST, dummy ID & secret key as parameters for this tutorial. You need to change it .LIVE and enter the real ID and the secret key you have.

let mPosUi = MPUMposUi.initialize(with: .TEST, merchantIdentifier:"ID",merchantSecret:"secret")

Step 3:

Based on the device you have, you need to choose between the two. Implement the TCP or external accessory parameter with the required parameter as shown below. Enter the remote address in which your device is connected. Port remains the same as far as I know.

For Verifone readers via WiFi or Ethernet

// When using Verifone readers via WiFi or Ethernet, use the following parameters:let accessoryParameters = MPAccessoryParameters.tcpAccessoryParameters(with: .verifoneVIPA,remote:"192.168.254.123",port:16107,optionals:nil)

For Bluetooth Miura

//When using the Bluetooth Miura, use the following parameters:let accessoryParameters = MPAccessoryParameters.externalAccessoryParameters(with: .miuraMPI, protocol: “com.miura.shuttle”, optionals: nil)

Note: If you need to configure ingenico, let me know. I’ll add that too.

Step 4:

In the transaction parameter we need to pass the amount and currency. The rest are up to you.

let transactionParameters = MPTransactionParameters.charge(withAmount: NSDecimalNumber(string: "100"),currency:MPCurrency.USD,optionals:{ (optionals:MPTransactionParametersOptionals!) inoptionals.subject = “Automobile Purchase”optionals.customIdentifier = “yourReferenceForTheTransaction”

Step 5:

Configure the appearance of the navigationBarTint and navigationBarTextColor using the mPosUi object we created. I always use colorLiteral, it’s a friendly way to choose color.

mPosUi.configuration.appearance.navigationBarTint = colorLiteral(red: 0.05154533684, green: 0.1255731881, blue: 0.2839779854, alpha: 1)mPosUi.configuration.appearance.navigationBarTextColor = colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

Step 6:

Configure the terminal parameter, the accessory parameter we created already should be passed here. Configure the Summary feature as you wish. You can email receipt or enable a refund or print receipt or capture a transaction.

mPosUi.configuration.terminalParameters = accessoryParametersmPosUi.configuration.summaryFeatures = MPUMposUiConfigurationSummaryFeature.sendReceiptViaEmail

Step 7:

Finally, create the transaction viewcontroller with the transaction parameter using the mPosUi object. Use the MPUTransactionResult in the completion handler to know the approval status.

let viewController:UIViewController! = mPosUi.createTransactionViewController(with: transactionParameters,completed:{ (controller:UIViewController,result:MPUTransactionResult,transaction:MPTransaction?)inself.dismiss(animated: true, completion:nil)let alert = UIAlertController(title: "Result", message: "", preferredStyle: .alert)let alertAction = UIAlertAction(title: "OK", style: .default, handler: nil)alert.addAction(alertAction)if result == MPUTransactionResult.approved {alert.message = "Payment was approved!"} else {alert.message = "Payment was declined/aborted!"}self.present(alert, animated: true, completion: nil)})

🎥Check this video for how it should work.

That’s it guys, time for me to leave.

Sample swift project for you guys to test:

https://github.com/Rajaikumar-iOSDev/Payworks-Swift

--

--

Rajai Kumar
Nerd For Tech

I’m a workaholic iOS developer. I focus a lot on UX and product efficiancy.